home *** CD-ROM | disk | FTP | other *** search
/ Robotics & Artificial Int…3 (Professional Edition) / Robotics & Artificial Intelligence Tools 2003 (Professional Edition).iso / neural network tool and application / nsinstall.exe / data1.cab / DLLCust_Files / PREPOST / DECIMATE.C < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-08  |  2.8 KB  |  94 lines

  1. // Dynamic link library implementation of a decimator with running average filtering
  2.  
  3. #include "NSDLL.h" 
  4.  
  5. #define    buffer(i, j)    bufferData->data[j+(i)*bufferData->length]
  6.  
  7. typedef struct {
  8.     int count;
  9.     int length;
  10.     NSFloat *data;
  11. } DecimatorData;
  12.  
  13. /***************************/
  14. /* Activation of component */
  15. __declspec(dllexport) BOOL performPrePost(
  16.     DLLData    *instance,    // Pointer to instance data (may be NULL)
  17.     NSFloat    *input,     // Pointer to the input data
  18.     NSFloat    *output,     // Pointer to the output data
  19.     int rows,         // Number of rows of data
  20.     int cols,         // Number of cols of data
  21.     BOOL preprocessor    // Flag to indicate whether this is a preprocessor or postprocessor
  22.     )
  23. {
  24.     int i, j;
  25.     int N = getIntParameter(instance, 2, 1);
  26.     DecimatorData *bufferData = getUserData(instance);
  27.     NSFloat result;
  28.  
  29.     for (i=N-1; i>0; i--)  
  30.         for (j=0; j<bufferData->length; j++) 
  31.             buffer(i,j) = buffer(i-1,j);
  32.     for (j=0; j<bufferData->length; j++)
  33.         buffer(0,j) = input[j];
  34.     if (++bufferData->count >= N) {
  35.         bufferData->count = 0;
  36.         if (!preprocessor)
  37.             for (j=0; j<bufferData->length; j++)
  38.                 output[j] = 0.0f;
  39.         for (j=0; j<bufferData->length; j++) {
  40.             result = 0.0f;
  41.             for (i=0; i<N; i++)
  42.                 result += buffer(i,j);
  43.             output[j] = result/N;
  44.         }
  45.         return TRUE;
  46.     }
  47.     return FALSE;
  48. }
  49.  
  50. /******************************************/
  51. /* Called every time the network is reset */
  52. __declspec(dllexport) void networkReset(
  53.     DLLData    *instance    // Pointer to instance data (may be NULL) 
  54.     )
  55. {
  56.     int i, j;
  57.     int N = getIntParameter(instance, 2, 1);
  58.     DecimatorData *bufferData = getUserData(instance);
  59.  
  60.     for (i=0; i<N; i++)
  61.         for (j=0; j<bufferData->length; j++) 
  62.             buffer(i,j) = 0.0f;
  63.     bufferData->count = 0;
  64. }
  65.  
  66. /******************************************/
  67. /* Management of instance data (OPTIONAL) */
  68. __declspec(dllexport) DLLData *allocPrePost(
  69.     DLLData    *oldInstance,    // Pointer to the last instance if reallocating
  70.     int *rows,         // Number of rows of output data, can be changed to reflect a diffenent number for the input data
  71.     int *cols,         // Number of cols of output data, can be changed to reflect a diffenent number for the input data
  72.     BOOL preprocessor    // Flag to indicate whether this is a preprocessor or postprocessor
  73.     )
  74. {
  75.     DLLData *instance = allocDLLInstance(oldInstance);
  76.     DecimatorData *bufferData = calloc(1,sizeof(DecimatorData));
  77.     setParameterName(instance, 2, 1, "N", TRUE);
  78.     setIntParameter(instance, 2, 1, 4, FALSE);
  79.     bufferData->length = *rows * *cols;
  80.     bufferData->data = calloc(bufferData->length*getIntParameter(instance, 2, 1), sizeof(NSFloat));
  81.     setUserData(instance, bufferData);
  82.     return instance;
  83. }
  84.  
  85. __declspec(dllexport) void freePrePost(DLLData *instance)
  86. {
  87.     DecimatorData *bufferData = getUserData(instance);
  88.     if (bufferData) {
  89.         free(bufferData->data);
  90.         free(bufferData);
  91.     }
  92.     freeDLLInstance(instance); 
  93. }
  94.